const path = require('path')
const Koa = require('koa')
const koaBody = require('koa-body')
const cors = require('koa2-cors')
const logger = require('koa-logger')
const static = require('koa-static')
const config = require('./config')
const router = require('./router')

const { getServerIP } = require('./utils')

const app = new Koa()

app
  .use(cors())
  .use(
    koaBody({
      multipart: true,
      formidable: {
        keepExtensions: true
      }
    })
  )
  .use(logger())
  // 静态资源访问路径
  .use(static(path.resolve(__dirname, 'static')))

app.use(router.routes(), router.allowedMethods())

app.listen(config.server_port, () => {
  console.log(`server listen on http://${getServerIP()}:${config.server_port}`)
})
